//
//  EmoticonAttachment.swift
//  FaceKeyBoard
//
//  Created by cmh on 15/9/13.
//  Copyright © 2015年 cmh. All rights reserved.
//

import UIKit

class EmoticonAttachment: NSTextAttachment {
    
    var chs: String
    
    init(chs: String) {
    
        self.chs = chs
        super.init(data: nil, ofType: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    class func emoticonAttributeText(emoticon: Emoticon, font: UIFont) -> NSAttributedString{
        
        let attachment = EmoticonAttachment(chs: emoticon.chs!)
        
        // 图片
        attachment.image = UIImage(contentsOfFile: emoticon.imagePath)
        // 设置图片的高度
        let height = font.lineHeight
        
        // 图片的内部偏移
        attachment.bounds = CGRect(x: 0, y: -4, width: height, height: height)
        
        // 创建图片属性字符串
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        
        // 设置字体
        imageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        
        return imageText
    }
    
    
    

}
